import re
import requests
from prettytable import PrettyTable
import json


def get_movie_page(url):
    # 发现不加 header 获取内容是空；先尝试加 User-Agent；
    # 如果还是空，那就需要加更多的 header
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/'
                      '537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36',
    }

    r = requests.get(url, headers=headers)
    html = r.text

    re_s = '<li>(.*?)</li>'
    item_info = re.findall(re_s, html, re.DOTALL)

    movies = []
    for item in item_info:
        sort = re.findall(r'<em class="">(\d+)</em>', item)[0]
        title = re.findall('<span class="title">(.*)</span>', item)[0]
        url = re.findall('<a href="(.*)">', item)[0]
        master_u = re.findall('主演: (.*?)[ .].*<br>', item)
        master_u = master_u[0] if master_u else ''
        director_u = re.findall('导演: (.*?)&nbsp;', item)
        director_u = director_u[0] if director_u else ''
        year = re.findall(r'(\d{4}).*&nbsp;', item)[0]
        score = re.findall(
            '<span class="rating_num" property="v:average">(.*)</span>',
            item)[0]

        movies.append({
            'sort': sort,
            'title': title,
            'url': url,
            'master_u': master_u,
            'director_u': director_u,
            'year': year,
            'score': score
        })
    return movies

def print_for_tab(top_movies):
    """以 table 格式输出"""
    table =PrettyTable()
    table.field_names = ['排序', '电影名', '主演', '导演', '上映年代', '豆瓣评分', '地址']
    for movie in top_movies:
        table.add_row([movie['sort'], movie['title'], movie['master_u'],
                       movie['director_u'], movie['year'], movie['score'],
                       movie['url']])
    print(table)

def print_for_json(top_movies):
    """以 json 格式输出"""
    print(json.dumps(top_movies, indent=4, ensure_ascii=False))




if __name__ == '__main__':
    top_movies = []
    for i in range(4):
        start  = i * 25
        url = f'https://movie.douban.com/top250?start={start}&filter='
        top_movies.extend(get_movie_page(url))

    print_for_tab(top_movies)
    print_for_json(top_movies)
